package com.yanqu.road.server.logic.recharge;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.recharge.RechargeOrder;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.RechargeModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class TenDayDiscountMallRecharge extends BaseRecharge{


    public TenDayDiscountMallRecharge(GamePlayer player, MallInfo mallInfo, ActivityInfo activityInfo) {
        super(player, mallInfo, activityInfo);
    }

    @Override
    protected boolean isRepeatedBuy(String orderNo) {
        return player.getModule(ActivityMallModule.class).isOverLimitBuyCount(mallInfo, 1);
    }

    @Override
    protected void repeatedBuyLogic() {

    }

    @Override
    protected String addMall(String orderNo, RechargeOrder rechargeOrder) {
        String rewardStr = "";
        String extendParam = mallInfo.getExtendParam();
        List<Integer> integerList = StringUtils.stringToIntegerList(extendParam, "\\|");
        if (integerList.size() != 3){
            logger.error("订单: {}, 直购现金礼包购买 十日特惠 商品: {}  extendParam: {}  解析错误", orderNo, mallInfo.getMallId(),extendParam);
            return rewardStr;
        }
        if (integerList.get(0) == 1){
            int ret = player.getModule(RechargeModule.class).dealPackageTenDayDiscountMall(mallInfo.getActivityId());
            if (ret != 0){
                logger.error("订单: {}, 直购现金礼包 打包购买 十日特惠 商品: {} 获取玩家condition更新失败", orderNo, mallInfo.getMallId());
                return rewardStr;
            }
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(mallInfo.getActivityId());
            if (activityConfigMap != null){
                if (activityConfigMap.get("TEN_DAY_DISCOUNT_MALL_PACKAGE_BUY_EXTRA_REWARD") != null){
                    rewardStr = activityConfigMap.get("TEN_DAY_DISCOUNT_MALL_PACKAGE_BUY_EXTRA_REWARD").getValue();
                }
            }
            player.getModule(CurrencyModule.class).addCurrency(rewardStr, eLogMoneyType.Recharge, eLogMoneyType.ReChargeGet);

        }else if(integerList.get(0) == 0){
            UserActivityConditionData userTenDayDiscountActivityConditionData = player.getModule(RechargeModule.class).getUserTenDayDiscountActivityConditionData(mallInfo.getActivityId(), integerList.get(2), integerList.get(1));
            if (userTenDayDiscountActivityConditionData == null){
                logger.error("订单: {}, 直购现金礼包购买 十日特惠 商品: {}  获取玩家condition失败", orderNo, mallInfo.getMallId());
                return rewardStr;
            }
            userTenDayDiscountActivityConditionData.setValue(BigInteger.ONE);
            BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(activityInfo.getActivityId());
            BaseActivityCondition activityCondition = baseActivityData.getBaseActivityCondition(userTenDayDiscountActivityConditionData.getConditionId());
            if (activityCondition == null){
                logger.error("订单: {}, 直购现金礼包购买 十日特惠 商品: {}  获取玩家condition失败", orderNo, mallInfo.getMallId());
                return rewardStr;
            }
            rewardStr = activityCondition.getReward();
            return rewardStr;

        }

        logger.info("订单: {}, 直购现金礼包购买 十日特惠 商品: {}", orderNo, mallInfo.getMallId());
        return rewardStr;
    }
}
